﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace AnthStat.Infrastructure
{
    //public abstract class XDocumentProvider
    //{
    //    // not thread safe yet
    //    private static bool pendingChanges;

    //    private bool _documentLoadedFromFile;

    //    //FileSystemWatcher fileWatcher;

    //    public static XDocumentProvider Default;

    //    public event EventHandler CurrentDocumentChanged;

    //    private XDocument _loadedDocument;

    //    public string FileName { get; set; }


    //    protected XDocumentProvider()
    //    {
    //        //fileWatcher = new FileSystemWatcher();
    //        //fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
    //        //fileWatcher.Changed += fileWatcher_Changed;
    //    }

    //    //void fileWatcher_Changed(object sender, FileSystemEventArgs e)
    //    //{
    //    //    if (_documentLoadedFromFile && !pendingChanges)
    //    //    {
    //    //        GetDocument(true);
    //    //    }
    //    //}


    //    /// <summary>
    //    /// Returns an open XDocument or create a new document
    //    /// </summary>
    //    /// <returns></returns>
    //    public XDocument GetDocument(bool refresh = false)
    //    {
    //        if (refresh || _loadedDocument == null)
    //        {
    //            // we need to refactor it, but just to demonstrate how should work I will send this way ;P
    //            //if (File.Exists(FileName))
    //            //{
    //                _loadedDocument = XDocument.Load(FileName);
    //                _documentLoadedFromFile = true;

    //                //if (fileWatcher.Path != Environment.CurrentDirectory)
    //                //{
    //                //    fileWatcher.Path = Environment.CurrentDirectory;
    //                //    fileWatcher.Filter = FileName;
    //                //    fileWatcher.EnableRaisingEvents = true;
    //                //}
    //            //}
    //            //else
    //            //{
    //            //    _loadedDocument = CreateNewDocument();
    //            //    fileWatcher.EnableRaisingEvents = false;
    //            //    _documentLoadedFromFile = false;
    //            //}

    //            if (CurrentDocumentChanged != null) CurrentDocumentChanged(this, EventArgs.Empty);
    //        }

    //        return _loadedDocument;
    //    }

    //    /// <summary>
    //    /// Creates a new XDocument with a determined schemma.
    //    /// </summary>
    //    public abstract XDocument CreateNewDocument();

    //    public void Save()
    //    {
    //        if (_loadedDocument == null)
    //            throw new InvalidOperationException();

    //        try
    //        {
    //            // tells the file watcher that he cannot raise the changed event, because his function is to capture external changes.
    //            pendingChanges = true;

    //            StringBuilder sb = new StringBuilder();
    //            XmlWriterSettings xws = new XmlWriterSettings();
    //            xws.OmitXmlDeclaration = false;
    //            xws.Indent = true;

    //            using (XmlWriter xw = XmlWriter.Create(sb, xws))
    //            {
    //                _loadedDocument.Save(xw);
    //            }

    //            //_loadedDocument.Save(FileName);
    //        }
    //        finally
    //        {
    //            pendingChanges = false;
    //        }
    //    }
    //}
}
